package hello;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
/**
 *
 * @author HOME
 */
public class PlayerBom extends Sprite{
    static final int WIDTH = 18;
    static final int HEIGHT = 30;
    static final int[] FRAME_SEQUENCE = { 0,1,2,3,4 };
    static Image ImagePhai;
    static Image ImageTrai;
    static Image ImageLen;
    static Image ImageXuong;
    static Image ImagieDie;
    int preX ;
    int preY;
    int LoaiHinh;
    int speed ;
    public int NumBom = 3;
    private JumpManager jump_manager ;
    boolean IsAlive ;
     int AliveCount =8;
    public PlayerBom(int x, int y,JumpManager manager)throws Exception
    {
        
        super(Image.createImage("/images/player/re_phai.png"), 
	  WIDTH, HEIGHT);
        
        
        //Image a = Image.createImage("/images/cowboy.png");
        //super(a,WIDTH,HEIGHT);
        int myInitialX = x + WIDTH/2;
        int myInitialY = y;
        // we define the reference pixel to be in the middle 
        // of the cowboy image so that when the cowboy turns 
        // from right to left (and vice versa) he does not 
        // appear to move to a different location.
        defineReferencePixel(WIDTH/2, 0);
        setRefPixelPosition(myInitialX, myInitialY);
        //setFrameSequence(FRAME_SEQUENCE);
            
        ImagePhai = Image.createImage("/images/player/re_phai.png");
        ImageTrai = Image.createImage("/images/player/re_trai.png");
        ImageLen = Image.createImage("/images/player/di_len.png");
        ImageXuong = Image.createImage("/images/player/di_xuong.png");
        ImagieDie = Image.createImage("/images/player/chet.png");
       jump_manager = manager;
       defineCollisionRectangle(5, 15, 3, 10);
               speed = 4;
               IsAlive = true;
    }
    void advance(int tickCount, int Action) {
     
        if (IsAlive ==false)
        {
            if (AliveCount ==0)
            {
                this.setVisible(false);
                return;
            }
                if (AliveCount %2 ==0)
                {
                    setImage(ImagieDie,WIDTH, HEIGHT);
                    nextFrame();
                }
                
                
             AliveCount --;
            return ;
        }
        int x = getX();
        int y = getY();
        //boolean b = collidesWith(jump_manager.background,false);
        
        if (x <0)
        {
            setPosition(preX, preY);
            return ;
        }
        if (x > JumpManager.DISP_WIDTH-20)
        {
            x = JumpManager.DISP_WIDTH-20;
            setPosition(preX, preY);
            return ;
        }
        if (y <0)
        {
            y =0;
           setPosition(preX, preY);
            return;
        }
        if (y > JumpManager.DISP_HEIGHT-35)
        {
            y = JumpManager.DISP_HEIGHT-35;
            setPosition(preX, preY);
            return;
        }
        preX = x;
        preY =y;
        //int speed = 5 
        if(Action == 1)
        {
           
            setImage(ImagePhai,WIDTH, HEIGHT);
            setPosition(getX(), getY());
            move(speed,0);
           
            nextFrame();
            
        }
        if(Action == 2)
        {
            setImage(ImageTrai,WIDTH, HEIGHT);
            setPosition(getX(), getY());
            move(-speed,0);
             
            nextFrame();
        }
        if(Action == 3)
        {
            setImage(ImageLen,WIDTH, HEIGHT);
            setPosition(getX(), getY());
            move(0,-speed);
            
            nextFrame();
        }
        if(Action == 4)
        {
            //ImageXuong.
            setImage(ImageXuong, WIDTH, HEIGHT);
            setPosition(getX(), getY());
            move(0,speed);
            
            nextFrame();
        }
        
    }
    void GoBack(int Action)
    {
       //  int speed = 5;
        if(Action == 1)
        {
           
                 move(-speed,0);                      
        }
        if(Action == 2)
        {
                 move(speed,0);
            
        }
        if(Action == 3)
        {
                 move(0,speed);
                
        }
        if(Action == 4)
        {
                 move(0,-speed);
        }
    }
}
